package ext.tianma.validators;

import java.util.Locale;

import wt.access.AccessControlHelper;
import wt.access.AccessPermission;
import wt.fc.WTObject;
import wt.fc.WTReference;
import wt.org.WTPrincipal;
import wt.org.WTUser;
import wt.part.WTPart;
import wt.session.SessionHelper;
import wt.type.TypedUtility;
import wt.util.WTException;

import com.ptc.core.meta.common.TypeIdentifier;
import com.ptc.core.ui.validation.DefaultUIComponentValidator;
import com.ptc.core.ui.validation.UIValidationCriteria;
import com.ptc.core.ui.validation.UIValidationKey;
import com.ptc.core.ui.validation.UIValidationResult;
import com.ptc.core.ui.validation.UIValidationResultSet;
import com.ptc.core.ui.validation.UIValidationStatus;

public class RAWTransitFactoryValidator extends DefaultUIComponentValidator{
	private static final String D_VIEW = "Design";
	
	public UIValidationResultSet performFullPreValidation(UIValidationKey uivalidationkey, UIValidationCriteria uivalidationcriteria, Locale locale) throws WTException {
		UIValidationStatus uivalidationstatus = UIValidationStatus.HIDDEN;
		UIValidationResultSet uivresultSet = new UIValidationResultSet();
		boolean flag = false;
		
		if (uivalidationcriteria != null && uivalidationcriteria.getContextObject() != null) {
			WTReference wtreference = uivalidationcriteria.getContextObject();
			wt.fc.Persistable persistable = wtreference.getObject();
			WTUser curUser = (WTUser) SessionHelper.manager.getPrincipal();
			WTPart part = (WTPart) persistable;
			if(isObjectType(part,"Polarizer")||isObjectType(part,"PhantomPolarizer")||isObjectType(part,"RawMaterial"))
			{
				String state = part.getLifeCycleState().toString();
				if ((state.equals("INWORK")) || (state.equals("NewProduct")) || (state.equals("APPROVAL")) || (state.equals("MassProductionRelease"))){
					WTPrincipal principal = SessionHelper.manager.getPrincipal();

			        if (principal != null){
			        	boolean accessControl = AccessControlHelper.manager.hasAccess(principal, part, AccessPermission.ALL);
			            if (accessControl) {
			              flag = true;
			            }

			        }

			    }
				
				//add kwang 2013-12-04 如果不是“Design”入口不显示
//				String view = part.getViewName();
//				if( ( view != null ) && ( !view.equals( D_VIEW ) ) ){
//					flag = false;
//				}
			}
		}
		if (flag)
		{
			uivresultSet.addResult(new UIValidationResult(uivalidationkey, UIValidationStatus.ENABLED));
		}
		else 
		{
			uivresultSet.addResult(new UIValidationResult(uivalidationkey, UIValidationStatus.HIDDEN));
		}
		return uivresultSet;
	}

	/**
	 *鍒ゆ柇瀵硅薄绫诲瀷鏄惁涓烘寚瀹氱殑鍐呴儴鍏抽敭瀛楃被鍨�
	 * @param object 瀵硅薄锛�
	 * @param object 鍐呴儴鍏抽敭瀛楋紱
	 * @return boolean  鏄寚瀹氱殑绫诲瀷杩斿洖true锛涗笉鏄繑鍥瀎alse
	 */
	public static boolean isObjectType(WTObject object,String objectType)
	{
		if(objectType.equals("") || objectType==null||object==null)
			return false;
		TypeIdentifier typeidentifier = TypedUtility.getTypeIdentifier(object);
		String typeName= typeidentifier.getTypename();
		if(typeName.indexOf(objectType)>=0)
		{
			return true;
		}
		return false;
	}

}
